package com.example.administrator.financialmanager;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.administrator.financialmanager.dao.InaccountDao;
import com.example.administrator.financialmanager.model.Tb_inaccount;

import java.util.Calendar;
import java.util.UUID;

public class AddIncomeActivity extends AppCompatActivity {
    protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
    EditText add_income_money_input, add_income_time_input, add_income_payer_input, add_income_describe_input;// 创建4个EditText对象
    Spinner add_income_kind_input;// 创建Spinner对象
    Button add_income_admit;// 创建Button对象“保存”
    Button add_income_cancel;// 创建Button对象“取消”

    private int mYear;// 年
    private int mMonth;// 月
    private int mDay;// 日

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_income);// 设置布局文件

        if(NavUtils.getParentActivityName(AddIncomeActivity.this)!=null)
            getActionBar().setDisplayHomeAsUpEnabled(true);

        add_income_money_input = (EditText) findViewById(R.id.add_income_money_input);// 获取金额文本框
        add_income_time_input = (EditText) findViewById(R.id.add_income_time_input);// 获取时间文本框
        add_income_payer_input = (EditText) findViewById(R.id.add_income_payer_input);// 获取付款方文本框
        add_income_describe_input = (EditText) findViewById(R.id.add_income_describe_input);// 获取备注文本框
        add_income_kind_input = (Spinner) findViewById(R.id.add_income_kind_input);// 获取类别下拉列表
        add_income_admit = (Button) findViewById(R.id.add_income_admit);// 获取保存按钮
        add_income_cancel = (Button) findViewById(R.id.add_income_cancel);// 获取取消按钮

        add_income_time_input.setOnClickListener(new View.OnClickListener() {// 为时间文本框设置单击监听事件
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
            }
        });

        add_income_admit.setOnClickListener(new View.OnClickListener() {// 为保存按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String strInMoney = add_income_money_input.getText().toString();// 获取金额文本框的值
                if (!strInMoney.isEmpty()) {// 判断金额不为空
                    // 创建InaccountDAO对象
                    InaccountDao inaccountDAO = new InaccountDao(AddIncomeActivity.this);
                    // 创建Tb_inaccount对象
                    Tb_inaccount tb_inaccount = new Tb_inaccount(
                            UUID.randomUUID().toString(),
                            Double.parseDouble(strInMoney),
                            add_income_time_input.getText().toString(),
                            add_income_kind_input.getSelectedItem().toString(),
                            add_income_payer_input.getText().toString(),
                            add_income_describe_input.getText().toString());
                    inaccountDAO.add(tb_inaccount);// 添加收入信息
                    // 弹出信息提示
                    Toast.makeText(AddIncomeActivity.this, "〖新增收入〗数据添加成功！",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AddIncomeActivity.this, "请输入收入金额！",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        add_income_cancel.setOnClickListener(new View.OnClickListener() {// 为取消按钮设置监听事件
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                add_income_money_input.setText("");// 设置金额文本框为空
                add_income_money_input.setHint("0.00");// 为金额文本框设置提示
                add_income_time_input.setText("");// 设置时间文本框为空
                add_income_time_input.setHint("2015-01-01");// 为时间文本框设置提示
                add_income_payer_input.setText("");// 设置付款方文本框为空
                add_income_describe_input.setText("");// 设置备注文本框为空
                add_income_kind_input.setSelection(0);// 设置类别下拉列表默认选择第一项
            }
        });

        final Calendar c = Calendar.getInstance();// 获取当前系统日期
        mYear = c.get(Calendar.YEAR);// 获取年份
        mMonth = c.get(Calendar.MONTH);// 获取月份
        mDay = c.get(Calendar.DAY_OF_MONTH);// 获取天数

        updateDisplay();// 显示当前系统时间
    }

    @Override
    protected Dialog onCreateDialog(int id){// 重写onCreateDialog方法
        switch (id) {
            case DATE_DIALOG_ID:// 弹出日期选择对话框
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                        mDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                    mYear = year;// 为年份赋值
                    mMonth = monthOfYear;// 为月份赋值
                    mDay = dayOfMonth;// 为天赋值
                    updateDisplay();// 显示设置的日期
                }
            };

    private void updateDisplay() {
        // 显示设置的时间
        add_income_time_input.setText(new StringBuilder().append(mYear).append("-")
                .append(mMonth + 1).append("-").append(mDay));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == android.R.id.home) {

            if(NavUtils.getParentActivityName(AddIncomeActivity.this)!=null)
                NavUtils.navigateUpFromSameTask(AddIncomeActivity.this);

            return true;
        }
        else
            return super.onOptionsItemSelected(item);
    }
}
